﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.IService.Design;
using Zhp.Model.Design;

namespace Zhp.Api.Controllers.Design
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiGroup(GroupName = ApiGroupEnum.Design)]
    [SeedMenu(MenuGroup = ApiGroupEnum.Design, Name = "库表管理", Order = 1, RouterName = "designtable", WebFilePath = "design/designTable/index", Icon = "database")]
    public class DesignTableController : ControllerBase
    {
        private IDesignTableService _designTableService;
        public DesignTableController(IDesignTableService designTableService)
        {
            _designTableService = designTableService;
        }

        [HttpGet]
        [SeedList("数据库表列表", "库表管理", ApiGroupEnum.Design)]
        public async Task<List<DesignTableModel>> Query()
        {
            return await _designTableService.Query();
        }

        [HttpPost]
        [SeedButton("新增表", "数据库表列表", "库表管理", ApiGroupEnum.Design, ["/api/designtable/queryDataTypeSelect"])]
        public async Task<bool> Create(DesignTableEdit p)
        {
            return await _designTableService.Create(p);
        }

        [HttpPost]
        [SeedButton("修改表", "数据库表列表", "库表管理", ApiGroupEnum.Design, ["/api/designtable/querycolumn", "/api/designtable/queryDataTypeSelect"])]
        public async Task<bool> Update(DesignTableEdit p)
        {
            return await _designTableService.Update(p);
        }

        [HttpPut]
        [SeedButton("修改表名称", "数据库表列表", "库表管理", ApiGroupEnum.Design)]
        public async Task<bool> TableRename(DesignTableRename p)
        {
            return await _designTableService.TableRename(p);
        }

        [HttpGet("{tableName}")]
        public async Task<List<DesignColumnModel>> QueryColumn(string tableName)
        {
            return await _designTableService.QueryColumn(tableName);
        }

        [HttpGet]
        public async Task<List<DesignColumnDataType>> QueryDataTypeSelect()
        {
            return await Task.FromResult(_designTableService.QueryDataTypeSelect());
        }

        [HttpDelete("{tableName}")]
        [SeedButton("删除表", "数据库表列表", "库表管理", ApiGroupEnum.Design)]
        public async Task<bool> Delete(string tableName)
        {
            return await _designTableService.Delete(tableName);
        }
    }
}
